package pathfinder;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JButton.*;
import javax.swing.plaf.basic.BasicArrowButton;
import javax.swing.event.*;
import javax.swing.JOptionPane;
import java.util.StringTokenizer;

class MyPanel extends JPanel {

    public MyPanel() {
        setBorder(BorderFactory.createLineBorder(Color.black));
        setBackground(Color.WHITE);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(700,700);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);       
        PathFinder.world.draw(g);
    }
}

public class MyFrame extends JFrame 
{
    private int add_box_mode;
    private int add_robo_mode;
    private Point robo_start_point;
    
    public MyFrame()
    {
        super();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        JButton playButton = new JButton("Play");
        playButton.setSize(10, 60);
        JButton pauseButton = new JButton("Pause");
        pauseButton.setSize(10, 60);
        JButton resetButton = new JButton("Reset");
        resetButton.setSize(10, 60);

        JButton addRobotButton = new JButton("Add Robot");
        addRobotButton.setSize(10, 60);
        JButton addBoxOne = new JButton("Add Box1");
        addBoxOne.setSize(10,60);
        JButton addBoxTwo = new JButton("Add Box2");
        addBoxTwo.setSize(10,60);
        JButton addBoxThree = new JButton("Add Box3");
        addBoxThree.setSize(10,60);
                        
        
        JLabel filler_label1 = new JLabel();
        JLabel filler_label2 = new JLabel();
        JLabel filler_label3 = new JLabel();
        JLabel filler_label4 = new JLabel();
        JLabel filler_label5 = new JLabel();
        JPanel buttonsPanel = new JPanel();
        buttonsPanel.setLayout(new GridLayout(3,15,10,10)); //second number is columns
        buttonsPanel.setSize(200,300);
        buttonsPanel.add(playButton);
        buttonsPanel.add(pauseButton);
        buttonsPanel.add(resetButton);
        buttonsPanel.add(addBoxOne);
        buttonsPanel.add(addBoxTwo);
        buttonsPanel.add(addBoxThree);
        buttonsPanel.add(addRobotButton);
       
        
        ///////////////////////////////////////////////
        
        JPanel controlPanel = new JPanel();
        controlPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        controlPanel.setSize(300,300);
        controlPanel.add(buttonsPanel);
        
        MyPanel drawingArea = new MyPanel();
        drawingArea.setLayout(new BorderLayout());
        drawingArea.setSize(500,500);
        add(controlPanel, BorderLayout.EAST);
        add(drawingArea, BorderLayout.WEST);
        
    
       drawingArea.addMouseListener(new MouseAdapter()
      {
            @Override
            public void mousePressed(MouseEvent e)
            {
                Point newPoint = new Point(getMousePosition());
                System.out.println("newPoint position"+newPoint.x+ newPoint.y);
                
                if(add_box_mode != 0)
                {
                    PathFinder.world.AddBlock(newPoint, add_box_mode);
                    setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
                    add_box_mode = 0;
                }
                else if(add_robo_mode == 1)
                {
                    robo_start_point = new Point(getMousePosition());
                    setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
                    add_robo_mode = 2;
                }
                else if(add_robo_mode == 2)
                {
                    PathFinder.world.newRobot(robo_start_point, new Point(getMousePosition()));
                    setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
                    add_robo_mode = 0;
                }
               PathFinder.mainFrame.repaint();
           }
       });
       
     
        playButton.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e){
               if(PathFinder.input_debug) System.out.println("Play button was clicked.");
               PathFinder.world.UpdateWorld();
               PathFinder.start();
               add_robo_mode = 0;
               add_box_mode = 0;
            }
        });
        pauseButton.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e){
               if(PathFinder.input_debug) System.out.println("Pause button was clicked.");
               PathFinder.pause();
               add_robo_mode = 0;
               add_box_mode = 0;
            }
        });
        resetButton.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e){
               if(PathFinder.input_debug) System.out.println("Reset button was clicked.");
               add_robo_mode = 0;
               add_box_mode = 0;
               PathFinder.pause();
               PathFinder.world.reset();
               PathFinder.mainFrame.repaint();
            }
        });
        addRobotButton.addActionListener(new ActionListener(){

              public void actionPerformed(ActionEvent e){
                 System.out.println("start Button was Clicked");
                 PathFinder.world.UpdateWorld();
                 setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
                 add_robo_mode = 1;
              }
          });
        
         addBoxOne.addActionListener(new ActionListener(){

              public void actionPerformed(ActionEvent e){
                 System.out.println("addBoxOne was Clicked"); 
                 setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
                 add_box_mode = 1;
              }
          });

        addBoxTwo.addActionListener(new ActionListener(){

             public void actionPerformed(ActionEvent e){
                 System.out.println("addBoxTwo Button was Clicked");  
                 setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
                 add_box_mode = 2;
              }
          });
        addBoxThree.addActionListener(new ActionListener(){

             public void actionPerformed(ActionEvent e){
                 System.out.println("addBoxThree Button was Clicked");  
                 setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
                 add_box_mode = 3;
              }
             
          });
    }
}
